/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cpproject1;


class Flag implements Runnable {

    @Override
    public void run() {
        this.time = System.currentTimeMillis();
        while (true) {
            System.out.println ("Flag timer: " + (System.currentTimeMillis() - this.time));
            if (System.currentTimeMillis() - this.time > this.winTime
                    && this.playerWithFlag != null) {
                
                this.maze.weHaveAWinner();
                break;
            } else {
                try {
                    Thread.sleep(500);	//Think of alternative for sleep try catch
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
    }

    public Flag(IMaze maze) {
        this.playerWithFlag = null;
        this.maze = maze;
    }
    private IMaze maze;
    private final int winTime = Constants.winTime;
    Player playerWithFlag;
    volatile long time;		//will making it volatile work?

    public synchronized void resetTimer() //as time has data race with run()
    {
        this.time = System.currentTimeMillis();
    }

    public synchronized void setPlayer(Player player) {
        this.playerWithFlag = player;
        this.time = System.currentTimeMillis();
    }
    
}
